using System;
using Comdiv.Extensions;
using Comdiv.Model;
using Comdiv.Model.Interfaces;
using Comdiv.Logging;
using Comdiv.Application;
using Comdiv.Persistence;
using Comdiv.Inversion;
using Comdiv.Security.Acl;
using Comdiv.Conversations;
using Comdiv.IO;
using Comdiv.Security;
using System.Linq;

namespace Comdiv.Security{
    public class UsrRecord{
        public string Login{ get; set;}
        private string _name;
        public string Name{
            get{
                if(_name.noContent()) _name = Login;
                return _name;
            }
            set { _name = value; }
        }

        public string Domain { get; set;}
        public string Occupation { get; set;}

        public string DomainId { get; set; }

        private const string fiopattern = @"^(\w+)\s+(\w+)\s+(\w+)$";
        public string GetFio(){
            if(Name.noContent()) return Login;
            if(Name.like(fiopattern)){
                return Name.replace(fiopattern, m => "{0} {1}.{2}."._format(m.Groups[1].Value,
                                                                            m.Groups[2].Value.Substring(0, 1).ToUpper(),
                                                                            m.Groups[3].Value.Substring(0, 1).ToUpper()));
            }
            return Name;
        }

    }
    public interface IUsrRecordResolver{
        UsrRecord Get(string login);
    }

    public class StubUsrRecordResolver : IUsrRecordResolver{
        public UsrRecord Get(string login){
            lock (this){
                return new UsrRecord{Login = login};
            }
        }
    }
}